﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;


public partial class Login : System.Web.UI.Page
{
    string connString = "server=HOME; database=bookStore; integrated security=true";
    string sql_Login = "select * from Users where user=@user AND password=@password";

    private bool isValidUser(string _user, string _password)
    {
        SqlConnection con = new SqlConnection(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand(sql_Login, con);
        cmd.Parameters.Add("@user", SqlDbType.VarChar);
        cmd.Parameters.Add("@password", SqlDbType.VarChar);
        cmd.Parameters["@user"].Value = _user;
        cmd.Parameters["@password"].Value = _password;
        SqlDataReader sr = cmd.ExecuteReader();
        if (sr.Read())
        {
            return true;
        }
        return false;
    }
    private void login(string user, string password)
    {
        if (isValidUser(user, password))
        {
            lblLoginMessage.Text = "Success";
            FormsAuthentication.SetAuthCookie(txtUser.Text.Trim(), true);
            Session["userName"] = txtUser.Text.Trim();
            Response.Redirect(FormsAuthentication.GetRedirectUrl(Session["userName"].ToString(),true));
            
        }
        else
        {
            lblLoginMessage.Visible = true;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        pnlloginMessage.Visible = false;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (this.IsValid)
        {
            if (txtUser.Text == "abc" && txtPassword.Text == "abc")
            {

            }
            else
            {
                pnlloginMessage.Visible = true;
            }
            //loginMessage.Visible = false;
            //login(txtUser.Text.Trim(), txtPassword.Text.Trim());
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}